package com.bw.test_reflect_annotation

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.bw.test_reflect_annotation.annotation.Entity
import com.bw.test_reflect_annotation.annotation.Field

/**
 * 元注解：
 *  @Target - 指定注解的作用目标
 *  @Retention - 指定注解的保留策略 源码<字节码文件<运行时
 *
 *
 */
class MainActivity2 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main2)
        //1:获得class对象
        //apt技术扫描带有@Entity注解的类
        val clazz = Class.forName("com.bw.test_reflect_annotation.Goods")
        //反射获得类上的注解
        val entity = clazz.getAnnotation(Entity::class.java) as Entity
        val value = entity.value
        print("表名 $value")
        val str = StringBuffer()
        str.append("create table $value(")
        //获得属性
        val fields = clazz.declaredFields
        for (field in fields) {
            //判断属性上是否有@Column注解
            val column = field.getAnnotation(Field::class.java)
            if (column != null) {
                val name = column.name
                val type = column.type
                val length = column.length
                str.append("$name $type($length),")
            }
        }
        str.deleteCharAt(str.length - 1)
        str.append(")")
        println("sql: $str")
        
        


    }
}